/**
 * Copyright (c) Martijn Grootens (m.e.grootens@student.utwente.nl), 
 * University of Twente.
 * Created july 2012.
 *
 * This file is part of dbcleaner.
 * 
 * dbcleaner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * dbcleaner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with dbcleaner.  If not, see <http://www.gnu.org/licenses/>.
 */

package dbcleaner.core.dbentries;

import dbcleaner.core.bibfields.*;

/**
 * Representation of a BibTeX reference of class "book".
 */
public class Book extends Entry {
    
    // class name
    public static final String ENTRY_CLASS = "book";
    
    // obligatory fields
    private static final String[] fields = {"author", "title", "publisher", "year"};
    
    /**
     * Constructor of a new and empty Book.
     * (This actually is duplicate code.)
     */
    public Book () {
        
        // Add the obligatory BibFields, all set to EMPTY.
        for (String ff : fields) {
            add_oblig_field(new BibField(ff));
        }
        
    }
    
    /**
     * Get the class name of this BibTeX reference.
     * (This is duplicate code which overrides super; 
     * it would be better to only override the variable.)
     */
    public String get_class_name () {
        return ENTRY_CLASS;
    }
    
    /**
    * Adds a BibField to this Book.
     * The Book class is special, since it has to have either an author,
     * AND/OR an editor; override the super method.
     * If to_add is not one of these special fields, just call super.
     * (This is duplicate could; also used in InBook. Could be made general.)
     */
    public void add_BibField (BibField to_add) {
        
        // Check for the special field names.
        if (to_add.get_field_name().equals("editor") && this.get_field_contents("author").equals(BibField.EMPTY)) {
            
            // We got the editor field and the author field still is empty
            for (BibField b : fields_oblig) {
                
                if (b.get_field_name().equals("author")) {
                    fields_oblig.set(fields_oblig.indexOf(b), to_add);
                    break;
                }
                
            }
            
        }
        else {
            super.add_BibField(to_add);
        }
        
        
    }
    
}